package com.hlabexamples.fabrevealmenu;

import com.gc.materialdesign.views.CheckBox;
import com.hlab.fabrevealmenu.model.FABMenuItem;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;

import java.util.ArrayList;

/**
 * DemoCodeFraction
 *
 * @since 2021-05-14
 */
public class DemoCodeFraction extends BaseFraction {
    private static final String COLOR = "#1976d2";
    private ArrayList<FABMenuItem> items;
    private CheckBox cbTitle;
    private CheckBox cbShowOverlay;
    private CheckBox cbDouble;
    private CheckBox chSmall;
    private CheckBox cbFont;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        // 返回指定的页面
        return scatter.parse(ResourceTable.Layout_fraction_code, container, false);
    }

    /**
     * onStart
     *
     * @param intent intent
     */
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setXml(false);
        initFabMenu();
        initListeners();
        initCheckBox();
    }

    private void initCheckBox() {
        DependentLayout dlCbTitle = (DependentLayout) getFractionAbility().
                findComponentById(ResourceTable.Id_dl_chTitle);
        cbTitle = (CheckBox) getFractionAbility().findComponentById(ResourceTable.Id_chTitle);
        cbTitle.setBackgroundColor(COLOR);
        dlCbTitle.setClickedListener(component -> {
            if (getFabMenu() != null && (!getFabMenu().isShowing() || !cbShowOverlay.isCheck())) {
                cbTitle.setChecked(!cbTitle.isCheck());
                getFabMenu().setTitleVisible(cbTitle.isCheck());
            }
        });
        DependentLayout dlShowOverlay = (DependentLayout) getFractionAbility().
                findComponentById(ResourceTable.Id_dl_chOverlay);
        cbShowOverlay = (CheckBox) getFractionAbility().findComponentById(ResourceTable.Id_chOverlay);
        cbShowOverlay.setBackgroundColor(COLOR);
        dlShowOverlay.setClickedListener(component -> {
            if (getFabMenu() != null && !getFabMenu().isShowing()) {
                cbShowOverlay.setChecked(!cbShowOverlay.isCheck());
                getFabMenu().setShowOverlay(cbShowOverlay.isCheck());
            }
        });
        cbDouble = (CheckBox) getFractionAbility().findComponentById(ResourceTable.Id_chDouble);
        cbDouble.setBackgroundColor(COLOR);
        items = getFabMenuItems();
        DependentLayout dlDouble = (DependentLayout) getFractionAbility().
                findComponentById(ResourceTable.Id_dl_chDouble);
        dlDouble.setClickedListener(component -> {
            if (getFabMenu() != null && (!getFabMenu().isShowing() || !cbShowOverlay.isCheck())) {
                cbDouble.setChecked(!cbDouble.isCheck());
                initItems(cbDouble.isCheck());
                getFabMenu().closeMenu();
                getFabMenu().setMenuItems(items);
            }
        });
        DependentLayout dlFont = (DependentLayout) getFractionAbility().findComponentById(ResourceTable.Id_dl_chFont);
        cbFont = (CheckBox) getFractionAbility().findComponentById(ResourceTable.Id_chFont);
        cbFont.setBackgroundColor(COLOR);
        dlFont.setClickedListener(component -> {
            if (getFabMenu() != null && (!getFabMenu().isShowing() || !cbShowOverlay.isCheck())) {
                cbFont.setChecked(!cbFont.isCheck());
                getFabMenu().closeMenu();
                if (cbFont.isCheck()) {
                    getFabMenu().setMenuTitleTypeface("quicksand.otf");
                } else {
                    getFabMenu().setMenuTitleTypeface("");
                }
            }
        });
        DependentLayout dlChSmall = (DependentLayout) getFractionAbility().
                findComponentById(ResourceTable.Id_dl_ch_small);
        chSmall = (CheckBox) getFractionAbility().findComponentById(ResourceTable.Id_chSmall);
        chSmall.setBackgroundColor(COLOR);
        dlChSmall.setClickedListener(component -> {
            if (getFabMenu() != null && (!getFabMenu().isShowing() || !cbShowOverlay.isCheck())) {
                chSmall.setChecked(!chSmall.isCheck());
                getFabMenu().closeMenu();
                if (chSmall.isCheck()) {
                    getFabMenu().setSmallerMenu();
                } else {
                    getFabMenu().setNormalMenu();
                }
            }
        });
    }

    private void initItems(boolean isToShowDoubleItems) {
        items = new ArrayList<>();
        FABMenuItem fabMenuItem = new FABMenuItem(Constants.ATTACGMENTS, ResourceTable.Graphic_ic_attachment);
        fabMenuItem.setId(Constants.ATTACGMENTS_TYPE);
        FABMenuItem fabMenuItem1 = new FABMenuItem(Constants.IMAGES, ResourceTable.Graphic_ic_image);
        fabMenuItem1.setId(Constants.IMAGES_TYPE);
        FABMenuItem fabMenuItem2 = new FABMenuItem(Constants.PLACES, ResourceTable.Graphic_ic_place);
        fabMenuItem2.setId(Constants.PLACES_TYPE);
        FABMenuItem fabMenuItem3 = new FABMenuItem(Constants.EMOTION, ResourceTable.Graphic_ic_emoticon);
        fabMenuItem3.setId(Constants.EMOTION_TYPE);
        items.add(fabMenuItem);
        items.add(fabMenuItem1);
        items.add(fabMenuItem2);
        items.add(fabMenuItem3);
        if (isToShowDoubleItems) {
            items.add(fabMenuItem);
            items.add(fabMenuItem1);
            items.add(fabMenuItem2);
            items.add(fabMenuItem3);
        }
    }

    @Override
    public void onMenuItemSelected(Component view, int id) {
        setCodeText(items.get(id).getTitle() + "Clicked");
        super.onMenuItemSelected(view,id);
    }
}
